package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.dto.LimitShowDTO;
import com.cqut.service.ICompetitionAuditService;
import com.cqut.util.ResponseUtil;

@RestController
@RequestMapping("/api/competitionAudit")
public class CompetitionAuditController {
	
	  @Resource(name="competitionAuditService")
	  private ICompetitionAuditService competitionAuditService;

	  /**
	   * 初始化列表
	   * @param limitShowDTO
	   * @param errors
	   * @return
	   */
	   @RequestMapping(value = "/list", method = RequestMethod.GET)
	   public ResponseEntity<String> list(
			String competitionName,
			String competitionState,
	   		@Validated
	   		LimitShowDTO limitShowDTO, BindingResult errors) {
		   if(errors.hasErrors())
	           return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	        
	       Map<String, Object> result = new HashMap<String, Object>();
	       System.out.println(competitionName);
	       System.out.println(competitionState);
	       List<Map<String, Object>> competitionList = competitionAuditService.findCompetitionAuditsWithLimit(competitionName,competitionState,limitShowDTO);
	       long count = competitionAuditService.findCountOfCompetitionAudits(competitionName,competitionState);
	
	       result.put("rows", competitionList);
	       result.put("total", count);
	       System.out.println(count);
	       System.out.println(competitionList.toString());
	
	       return ResponseUtil.getResEntityForGetAndJson(result);
	   }
	
	   /**
	    * 审核通过
	    * **/
	   @RequestMapping(value = "/pass", method = RequestMethod.POST)
	   public ResponseEntity<String> passCompetitions(
			   @RequestParam("competitionsIds")
			   String[] competitionsIds){
		   for (int i = 0; i < competitionsIds.length; i++) {
			   System.out.println(competitionsIds[i]);
		}
		  
		   if(competitionsIds == null || 0 == competitionsIds.length)
				return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		   
		   if(competitionAuditService.passCompetitionAudits(competitionsIds))
			   return ResponseUtil.getResEntityForPPPWhenSuccess();
		   else 
			   return ResponseUtil.getResEntityForPPPWhenFail();
	   }
			   
	   
	   /**
	    * 审核退回
	    * **/
	   @RequestMapping(value = "/retreat", method = RequestMethod.POST)
	   public ResponseEntity<String> retreatCompetitions(
			   @RequestParam("competitionsIds")
			   String[] competitionsIds){
		 
		   if(competitionsIds == null || 0 == competitionsIds.length)
				return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		   
		   if(competitionAuditService.retreatCompetitionAudits(competitionsIds))
			   return ResponseUtil.getResEntityForPPPWhenSuccess();
		   else 
			   return ResponseUtil.getResEntityForPPPWhenFail();
	   }
	   
	 /**
	  * 竞赛详情
	  * **/
	  @RequestMapping(value = "/detail", method = RequestMethod.GET)
	  public ResponseEntity<String> competitionDetail(
			  
			  String competitionId){
		  System.out.println("this");
		  
		  if(competitionId == null){
			 
			  return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		  }
				
		
		  Map<String, Object> competition = competitionAuditService.getCompetitionDetailById(competitionId);
		  System.out.println(competition.toString());
		  return ResponseUtil.getResEntityForGetAndJson(competition);
	  }
	  
	  /**
		 * 得到所有的状态
		 * */
		@RequestMapping(value = "/state", method=RequestMethod.GET)
		public ResponseEntity<String> getRole() {
			//这里以后可能要根据登录人的信息来判断，现在先
			Map<String, Object> result = new HashMap<String, Object>();
			List<Map<String, Object>> states = competitionAuditService.getState();
			
			System.out.println("sdfsadfsadfsdf");
			System.out.println(states);
			
			result.put("rows", states);
			return ResponseUtil.getResEntityForGetAndJson(result);
		}
	  
	  
}
	